<?php

declare(strict_types=1);

namespace Modules\User\Enums;

/**
 * 系统模块名称枚举
 */
enum ModuleName: string
{
    case USER = 'user';
    case PERMISSIONS = 'permissions';
    case SYSTEM = 'system';
    case LOG = 'log';
    case COMMON = 'common';
    /**
     * 获取模块显示名称
     */
    public function label(): string
    {
        return match ( $this ) {
            self::USER => '用户管理',
            self::PERMISSIONS => '权限管理',
            self::SYSTEM => '系统管理',
            self::LOG => '日志管理',
            self::COMMON => '公共模块',
        };
    }

    /**
     * 根据命名空间自动获取模块枚举
     */
    public static function fromNamespace(string $namespace): self
    {
        if (preg_match('/^Modules\\\\(\w+)\\\\/', $namespace, $matches)) {
            $moduleName = strtolower($matches[1]);

            foreach (self::cases() as $case) {
                if ($case->value === $moduleName) {
                    return $case;
                }
            }
        }

        return self::COMMON; // 默认值
    }

    /**
     * 获取所有选项
     */
    public static function options(): array
    {
        return array_map(function (self $case) {
            return [
                'value' => $case->value,
                'label' => $case->label(),
            ];
        }, self::cases());
    }
}